/* 
 * StikiWeb Wiki - a Wiki for Hierarchical Organizations
 * $Id: AttachmentBean.java,v 1.3 2004/04/30 20:04:33 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the StikiWeb Wiki.
 *
 * The Initial Developer of the Original Code is Lee Haslup (biglee@haslups.com).
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.stikiweb.struts;

//import javax.servlet.http.HttpServletRequest;

//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
//import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
//import org.apache.struts.upload.MultipartRequestHandler;

/**
 * @author Allen L (BigLee) Haslup
 *
 */
public class AttachmentBean extends ActionForm {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2705500593465310432L;

	// page name to which file is to be attached.
	protected String name;
	
	// maximum size of file (for warning)
	protected String maxSize;
	
	// the file to attach to the page.
	protected FormFile theAttachment;

	/**
	 * @return
	 */
	public String getMaxSize() {
		return maxSize;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return
	 */
	public FormFile getTheAttachment() {
		return theAttachment;
	}

	/**
	 * @param string
	 */
	public void setMaxSize(String string) {
		maxSize = string;
	}

	/**
	 * @param string
	 */
	public void setName(String string) {
		name = string;
	}

	/**
	 * @param file
	 */
	public void setTheAttachment(FormFile file) {
		theAttachment = file;
	}

	/*
	 * This is commented out because it doesn't work in several different interesting ways...
	 * 
	 * 1)This does not appear to be called when the attachment is excessively large.
	 * Instead, control is passed to the AttachFileAction with the bean mostly 
	 * uninitialized  (name and theAttachment are null).  Dunno why...
	 * 
	 * 2) If the error list returned is non-empty an empty html page is sent to the browser.
	 * There is no error message that I have been able to find -- but the string
	 * "<html><body></body></html>" is sent to the browser.  Putting exactly the same
	 * code in the action and returning mapping.getInput() works like a champ...
	 * 
	 * Spent the better part of a day on this... *sigh*
	 */
	/*public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {
		
		// if the user has not pressed attach there is nothing to validate.
		if(req.getParameter("attach")==null) {
			return null;
		}
		
		ActionErrors errors = null;
		
		// some things we will check
		boolean okSoFar = true;
		boolean tooBig = false;
		boolean missingFile = false;
		
		//has the MultipartRequestHandler's maximum length been exceeded?
		Boolean maxLengthExceeded = (Boolean)
				req.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
		if((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
			tooBig = true;
			okSoFar = false;
		}
		
		// gotta have a file.
		if (okSoFar && theAttachment==null) {
			missingFile = true;
			okSoFar = false;
		}
		
		if(!okSoFar) {
			errors = new ActionErrors();
			if(missingFile&&false) {
				errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("error.upload.fileMissing"));
			}
			if(tooBig) {
				errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("error.upload.fileTooBig"));
			}
		}
		return errors;
	} */

}
